#include <math.h>

/* This primality test can be further improved. See
 * http://en.wikipedia.org/wiki/Primality_test */
int is_prime(double a)
{
	double i;

	// All even numbers except 2 are composite.
	if( a != 2 && fmod(a, 2) == 0 )
		return 0;

	/* TODO: check whether it's necessary to include sqrt(a) or can we just
	 * for ( i = 2; i < sqrt(a); i++ ). (I think it is). */
	for ( i = 2; i <= sqrt(a); i++ ) {
		if ( fmod(a, i) == 0 )
			return 0;
	}

	return 1;
}

/* Returns the NEXT bigger prime number to a given prime number. */
int next_prime(double prime)
{
	double i;

	i = prime;
	while( 1 ) {
		i++;
		if ( is_prime(i) )
			return i;
	}
}
